import { fakeRegister } from '../services/api';
import { setAuthority } from '../utils/authority';
import { reloadAuthorized } from '../utils/Authorized';
import { routerRedux } from 'dva/router';

export default {
  namespace: 'register',

  state: {
    code: undefined,
    data:{},
    message:''
  },


  effects: {
    *submit({payload}, { call, put }) {
      const response = yield call(fakeRegister,payload);
      console.log('register',response,'responsedata',response.data.roleName,'@roleName',
      response.data.roleName
    )
      if(response.code == 0){
        let userType = '';
        switch(response.data.roleName){
          case '管理员':
            userType = 'admin';
            break;
          case '开发':
            userType ='开发';
            break;
          case '测试':
            userType='测试';
            break;
          case '产品':
            userType='产品';
            break;
        }

       yield put({
        type: 'registerHandle',
        payload:{ currentAuthority: userType},
      });
      console.log('userType',userType)
      reloadAuthorized();
      yield put(routerRedux.push('/quality/workbanch'));
      }

    },
  },

  reducers: {
    registerHandle(state, { payload }) {
      setAuthority(payload.currentAuthority);
      
      return {
        ...state,
        ...payload,
      };
    },
  },
};
